<?php
/*
 * Created on May 15, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Contact extends VNT_Controller{
		function Contact(){
			parent::VNT_Controller();
			$this->load->library('xajax', 'contact');
			$this->xajax->registerFunction(array('reply', &$this, 'reply'));
			$this->xajax->processRequest();
			$this->xajax_js = $this->xajax->getJavascript($this->base_url.'js/');
			$this->load->model('contact_model', 'contact', true);
			$this->checkLogin();
			$this->checkPermission();
		}
		
		function index(){
			//echo $this->config->item('mailpath');
			//$this->sendMail('khoi.pham@anhone.vn', 'Test email', 'Day la noi dung thong diep');
			$this->view();
		}
		
						
		function edit($id){
			redirect('admin/contact/view');
			return;
			// Chua su dung chuc nang nay.
			
			$product = $this->contact->getById($id);
			
			if(!$product){
				redirect('admin/contact/view');
			}
			$this->load->model('unit_model', 'unit', true);
			$this->load->model('source_model', 'source', true);
			
			$select_unit = new stdClass();
			$select_unit->id = 0;
			$select_unit->vi_name = 'Select';
			$select_unit->en_name = 'Select';
			
			$select_source = new stdClass();
			$select_source->id = 0;
			$select_source->vi_name = lang('select_category');
			$select_source->en_name = lang('select_category');
			
			$data = array();
			$data['product'] = $product;
			$data['ddlCategory'] = $this->buidDDProductCategoryList('category', $product->code_pcategory); //echo  $product->code_pcategory . 'AAAAAA' . $product->vi_name;
			$data['units'] = array_merge(array($select_unit), $this->unit->getAll());
			$data['sources'] = array_merge(array($select_source), $this->source->getAll()); //print_r($data['units']);
			$data['error_message'] = '';
			$data['legend_text'] = lang('edit_product');
			$this->main = $this->load->view('admin/product/create_product', $data, true);
			$this->execute();
		}
		
		
		function view(){
			
			$this->load->library('pagination');
			
			$page = (int)$this->uri->segment(4);
			
			$base_url = $this->base_url . 'admin/contact/view/';
			$total_rows =  $this->contact->countAll($this->contact->countAll());
			$per_page = NUMBER_RECORD_PER_PAGE;
			$uri_segment = 4;
			pagination($base_url, $total_rows, $per_page , $uri_segment);
			
			$paging = $this->pagination->create_links();
			
			$rows = $this->contact->getAll($page, $per_page);
			
			if($page > 0 AND count($rows)==0){
				redirect('admin/contact/view/' . ($page-$per_page));
				return;
			}
			
			$data = array();
			$data['contacts'] = $rows;
			
			$this->main =  $this->load->view('admin/contact/contact_list', $data, true);
			
			$this->execute();
		}
		
		function detail($id){
		
			$contact = $this->contact->getById($id);
			
			if(!$contact){
				redirect('admin/contact');
				return;
			}
			
			if($contact->status == 0){
				$contact->status = 1;
				$contact->answer_date = date('Y-m-d H:m:i');
				$this->contact->update($contact);
			}
			
			$data = array();
			
			$data['contact'] = $contact;
			
			$this->main =  $this->load->view('admin/contact/contact_detail', $data, true);
			
			$this->execute();
		}
		
		function reply($id, $title, $content){
			$this->load->model('contact_model', 'contact', true);
			$contact = $this->contact->getById($id);
			
			$objResponse = new xajaxResponse();
			$objResponse->assign("box_reply", "style.display", 'block');
			$objResponse->assign("box_reply" , "innerHTML", $content);
			//$objResponse->assign("gnf" . $group_id, "innerHTML", $new_name);
			//return $objResponse->redirect('');
			return $objResponse;
		}
	 	
 	function delete(){
 		$id = intval($this->uri->segment(4));
		$ids = array();
 		if($id>0){
			$ids[] = $id;
 			//$this->product->delete($id);
		}
 		else{
 			if(($this->input->post('submitDelcategory'))){
 				$ids = $this->input->post('categoryBox');
 			 	
 			}
 		}
 		
		for($i = 0; $i <count($ids);$i++) echo $i . '<br>';
		if(count($ids)){
	
 			$result = $this->contact->delete($ids);
						
		}
		
 		redirect($_SESSION['his_adm']);
 	}
	
	function sendMail($to, $subject, $message){
		
		$this->load->library('email');
		$email_config = array();
		$email_config['protocol'] = $this->config->item('protocol');
		$email_config['mailpath'] = $this->config->item('mailpath');
		$email_config['charset'] = $this->config->item('charset');
		$email_config['wordwrap'] = $this->config->item('wordwrap');
		$email_config['smtp_user'] = $this->config->item('smtp_user');
		$email_config['smtp_pass'] = $this->config->item('smtp_pass');
		$email_config['smtp_host'] = $this->config->item('smtp_host');
		$email_config['smtp_port'] = $this->config->item('smtp_port');
				
		$this->email->initialize($email_config);

		$this->email->from($this->config->item('email_from'), $this->config->item('email_from_name'));
		$this->email->to($to);
		//$this->email->cc('khoi.pham@anhone.vn');
		//$this->email->bcc('them@their-example.com');
		
		$this->email->subject($subject);
		$this->email->message($message);
		
		return $this->email->send();
		
		echo $this->email->print_debugger();
		
		//echo $this->config->item('mailpath');
	}
 	
	}
?>
